/*** server ***/
package comm;

import java.net.*;
import java.io.*;
import main.Main;

public class Communication {

    private boolean isConnected;
    private static ServerSocket serverSocket = null;
    public boolean listening = true;

    public boolean isConnected() {
        return isConnected;
    }

    public void connect() {
        try {
            serverSocket = new ServerSocket(Main.getPort());
            System.err.println("Listenning to port: " + Main.getPort());
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + Main.getPort());
            System.exit(-1);
        }
        isConnected = true;
        Main.setState(0);
        try {
            while (listening) {
                new KKMultiServerThread(serverSocket.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {
            serverSocket.close();
            isConnected = false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
